package cn.hdy.org.service.impl;

import cn.hdy.base.service.impl.BaseServiceImpl;
import cn.hdy.base.util.PageList;
import cn.hdy.org.domain.Employee;
import cn.hdy.org.mapper.EmployeeMapper;
import cn.hdy.org.query.EmployeeQuery;
import cn.hdy.org.service.IEmployeeService;
import cn.hutool.crypto.SecureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "employee")
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService  {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    @CacheEvict(key = "'List'")
    public void add(Employee employee) {
        employee.setPassword(SecureUtil.md5(employee.getPassword()));
        employeeMapper.add(employee);
    }

    @Override
    @CacheEvict(key = "'List'")
    public void del(Long id) {
        employeeMapper.del(id);
    }

    @Override
    @CacheEvict(key = "'List'")
    public void patchDel(Long[] ids) {
        employeeMapper.patchDel(ids);
    }

    @Override
    @CacheEvict(key = "'List'")
    public void update(Employee employee) {
        employee.setPassword(SecureUtil.md5(employee.getPassword()));
        employeeMapper.update(employee);
    }

    @Override
    @Cacheable(key = "'List'")
    public List<Employee> findAll() {
        return  employeeMapper.findAll();
    }



    @Override
    public PageList<Employee> queryPage(EmployeeQuery query) {
        PageList<Employee> pageList = new PageList<>();
        Long count = employeeMapper.queryCount(query);
        if (count == null || count == 0) {
            return pageList;
        }
        List<Employee> employeeList = employeeMapper.queryData(query);
        pageList.setData(employeeList);
        pageList.setTotals(count);
        return pageList;
    }
}
